<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'文章列表'),
	array('op'=>'edit','title'=>'添加文章'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['adwei']){
		$params['adwei']=intval($_RQ['adwei']);
		$return['params']['adwei']=intval($_RQ['adwei']);
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=intval($_RQ['mid']);
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_wechatnews_news',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'merchname');
			$item['listtitle']=pdo_getcolumn('jy_wechatnews_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['listid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'list'=>array(
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'type','title'=>'发文类型','type'=>'radio','options'=>'news_type'),
			array('name'=>'listtitle','title'=>'公众号名称','type'=>'string'),
			array('type'=>'string','title'=>'所属商家','name'=>'merchname'),
			array('name'=>'jinbi','title'=>'发文金币','type'=>'string'),
			array('name'=>'price','title'=>'发文价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'updatetime','title'=>'刷新时间','type'=>'time'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'listtitle','width'=>15,'title'=>'文章标题','type'=>'string'),
			array('name'=>'type','width'=>15,'title'=>'发文类型','type'=>'radio','options'=>'news_type'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['news_type']=$this->news_type;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wechatnews_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回文章列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'文章名称','type'=>'string'),
			array('name'=>'image','title'=>'文章图片','type'=>'image'),
			array('type'=>'choose','title'=>'选择公众号','name'=>'listid','action'=>'jy_wechatnews/get_list/','choose_title'=>'选择公众号'),
			array('name'=>'mid','title'=>'选择发布用户','type'=>'choose','action'=>'core/member.get_member/'),	
			array('name'=>'desc','title'=>'文章介绍','type'=>'text'),
			array('name'=>'type','title'=>'发文类型','type'=>'radio','options'=>'news_type'),
			array('type'=>'jsup','width'=>50,'name'=>'content','title'=>'AI生成','action'=>'jy_wechatnews/news/aiup/','message'=>'点击《生成》将根据文章名称，文章介绍，生成一遍新文章，会覆盖下面的文章内容！','btntitle'=>'点击生成文章内容','show'=>array('type'=>0)),
			array('name'=>'content','title'=>'文章内容','type'=>'text','height'=>'600','show'=>array('type'=>0)),
			array('name'=>'content','title'=>'文章内容','type'=>'richtextold','show'=>array('type'=>1)),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['news_type']=$this->news_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写文章名称！','error');
	}
	if(!$postdata['merchid']){
		//exi('请选择发布的商户！','error');
	}
	if(!$postdata['listid']){
		exi('请选择发布的公众号！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'mid'=>intval($postdata['mid']),
		'type'=>intval($postdata['type']),
		'merchid'=>intval($postdata['merchid']),
		'listid'=>intval($postdata['listid']),
		'desc'=>trim($postdata['desc']),
		'content'=>trim($postdata['content']),
		'status'=>intval($postdata['status']),
		'jinbi'=>trim($postdata['jinbi']),
		'price'=>trim($postdata['price']),
		'noshow'=>intval($postdata['noshow']),
		'ishot'=>intval($postdata['ishot']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_wechatnews_news',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_wechatnews_news',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='aiup'){
	$postdata=get_postdata();
	$title=$postdata['title'];
	if(!$title){
		exi('请填写文章名称！','error');
	}
	$tomsg='写一篇标题为【'.$title.'】的文章！';
	$res['content']=$this->model('model')->aimsg($tomsg,true);
	exi($res);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_wechatnews_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
